#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 4e5 + 5;

int n, m;
int a[N], pos[N];
ll solve() {
  cin >> n;
  rep(i, 1, n * 2) cin >> a[i];
  rep(i, 1, n * 2) pos[a[i]] = i;
  a[0] = 0, a[n * 2 + 1] = -1;
  ll cnt = 0;
  rep(i, 1, n * 2) {
    int j = pos[a[i]];
    if (j == i || j == i + 1) continue;
    if (a[i - 1] == a[j - 1]) cnt++;
    if (a[i - 1] == a[j + 1]) cnt++;
    if (a[i + 1] == a[j - 1] && i + 2 < j - 1) cnt++;
    if (a[i + 1] == a[j + 1]) cnt++;
  }
  return cnt / 2;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) cout << solve() << endl;
  return 0;
}